package com.itzhigui.controller;

import com.itzhigui.entity.Student;
import com.itzhigui.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 定义控制器，作为页面跳转
 *
 * @author java1412
 * @since 2024-04-15 09:23:42
 */
@Controller
public class PageController {

    @Resource
    private StudentService studentService;

    // 方法的返回值就是逻辑视图名，自动去拼接前缀和后缀
    @RequestMapping(value = "/index")
    public String index(
            Integer age,
            Model model
    ) {
        // 向model中添加键值对
        model.addAttribute("loginUser", "admin");
        // 大于等于18，显示成年人
        // 小于18，显示未成年人
        model.addAttribute("age", age);
        // 调用业务层查询数据
        List<Student> studentList = studentService.findAll();
        model.addAttribute("studentList", studentList);
        // 添加日期对象
        model.addAttribute("myBirthday", new Date());
        // 添加jdk1.8的日期类型
        // model.addAttribute("myLocalDateTime", LocalDateTime.of(2024, 4, 15, 9, 44, 45));
        model.addAttribute("myLocalDateTime", LocalDateTime.now());
        return "index";
    }

}
